<?php

namespace App\Http\Controllers;

use App\Models\Appointment;
use App\Models\User;
use App\Models\WeddingDress;
use App\Models\WeddingDressType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class AdminController extends Controller
{
    function login(Request $req)
    {
        $rule = [
            'adminname' => 'required|min:4|max:6',
            'password' => 'required|min:6|max:16',
        ];
        $admin = [
            'adminname' =>  $req->adminname,
            'password'  =>  $req->password
        ];
        // 验证用户输入是否正确（格式）
        $check = Validator::make($admin, $rule);
        if ($check->fails()) {
            dd('账号最短4位,密码最短6位');
            return response()->json(['code' => 201, 'message' => '账号最短4位,密码最短6位']);
        }
        if (Auth::guard('admin')->attempt($admin,true)) {
            $req->session()->regenerate();
            return redirect('admin/home');
        } else {
            dd('账号或密码错误');
            return response()->json(['code' => 201, 'message' => '账号或密码错误']);
        }
    }

    function home()
    {
        return view('admin/home');
    }

    function weddingdress(){
        $dressinfo = WeddingDress::get();
        $weddingDressType = WeddingDressType::get();
        return view('iframe.weddingdress',['data' => $dressinfo, 'WeddingDressType' => $weddingDressType]); 
    }

    function rend(){
        $Appointmentinfo = Appointment::get();
        return view('iframe.Appointment',['data' => $Appointmentinfo]); 
    }

    function user(){
        $userinfo = User::get();
        return view('iframe.User',['data' => $userinfo]); 
    }

    function loginout(Request $req){
        Auth::guard('admin')->logout();
        $req->session()->invalidate();
        return redirect('/');
    }
}
